package net.jumpengine.util;

import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import net.jumpengine.enume.TextAlign;

public class UDraw {
	private static Graphics g;
	private static Image image;
	private static Map<String,java.awt.Font> fontMap=new HashMap<String,java.awt.Font>();
	
	static {
		image=new BufferedImage(10, 10, BufferedImage.TYPE_3BYTE_BGR);
		g=image.getGraphics();
	}
	
	/**
	 * 获取字体
	 * @param name 字体名称,为逗号分隔的字体
	 * @param size 尺寸
	 * @param bold 是否为粗体
	 * @param italic 是否为斜体
	 * @return
	 */
	public static Font getFont(String name,double size,boolean bold,boolean italic) {
		return Font.font(name, bold?FontWeight.BOLD:FontWeight.NORMAL, italic?FontPosture.ITALIC:FontPosture.REGULAR, size);
	}
	
	/**
	 * 绘制文字粗体
	 * @param str 文字
	 * @param x x坐标
	 * @param y y坐标
	 * @param g 绘制工具
	 */
	public static void boldString(String str,int x,int y,GraphicsContext g) {
		g.fillText(str,x-0.5, y);
		g.fillText(str,x,y-0.5);
		g.fillText(str, x, y);
	}
	
	public static Point centerString(String str,Font f,int width,int height) {
		return alignString(str, f, width, height, 0, TextAlign.center);
	}
	
	/**
	 * 获取颜色的反色
	 * @param c 颜色
	 * @return 反色
	 */
	public static Color antiColor(Color c) {
		return Color.color(1-c.getRed(), 1-c.getGreen(), 1-c.getBlue());
	}
	
	public static Point alignString(String str,Font f,int width,int height,int padding,TextAlign align) {
		java.awt.Font font=javaFxToAwt(f);
		FontMetrics metrics = g.getFontMetrics(font);
		int textHeight=metrics.getAscent()+metrics.getDescent()+metrics.getLeading();
		if(align==TextAlign.left) {
			return new Point(padding,(height-textHeight)/2);
		}
		int len = metrics.stringWidth(str);
		if(align==TextAlign.center) {
			return new Point((width-len)/2,(height-textHeight)/2);
		}
		if(align==TextAlign.right) {
			return new Point(width-len-padding,(height-textHeight)/2);
		}
		return null;
	}
	
	/**
	 * 获取字体高度
	 * @param f 字体
	 * @return 字体高度
	 */
	public static int fontHeight(Font f) {
		java.awt.Font font=javaFxToAwt(f);
		FontMetrics metrics = g.getFontMetrics(font);
		return metrics.getAscent()+metrics.getDescent()+metrics.getLeading();
	}
	
	public static int stringWidth(String str,Font f) {
		java.awt.Font font=javaFxToAwt(f);
		FontMetrics metrics = g.getFontMetrics(font);
		return metrics.stringWidth(str);
	}
	
	public static List<String> splitString(String str,int width,Font f){
		if(UComm.isNull(str))return null;
		List<String> list=new ArrayList<String>();
		int start=0;
		double half=f.getSize()/2,one=f.getSize(),len=0.0;
		char c;
		for(int i=0;i<str.length();i++) {
			c=str.charAt(i);
			if(c=='\n') {
				list.add(str.substring(start, i));
				start=i+1;
				len=0;
			}
			len+=c<256?half:one;
			if(len>=width || i==str.length()-1) {
				list.add(str.substring(start, i));
				start=i;
				len=0;
			}
		}
		return list;
	}
	
	private static java.awt.Font javaFxToAwt(Font f){
		String key=f.getFamily()+f.getSize()+f.getStyle();
		java.awt.Font font=fontMap.get(key);
		if(font==null) {
			int style=java.awt.Font.PLAIN;
			if(f.getStyle().indexOf("Bold")>=0)style=java.awt.Font.BOLD;
			if(f.getStyle().indexOf("Italic")>=0)style+=java.awt.Font.ITALIC;
			font=new java.awt.Font(f.getFamily(),style,(int)f.getSize());
			fontMap.put(key, font);
		}
		return font;
	}
}
